package generics_and_collections.stack;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * Created by Sam on 08/02/14.
 * FIFO.
 * IStack interface enforces Stack rules, defined as a list of elements
 * which can insert or delete elements only at the top of the stack.
 */

public class MyStack implements IStack {

    ArrayList arrayList = null;
    //Object top = null;

    public MyStack() {
        arrayList = new ArrayList();
    }

    @Override
    public boolean isEmpty() {
        return arrayList.size() == 0;
    }

    @Override
    public int size() {
        return arrayList.size();
    }

    @Override
    public void push(Object obj) {
        arrayList.add(arrayList.size(), obj);
    }

    @Override
    public Object pop() {
        if(!isEmpty()) {
            return arrayList.remove(arrayList.size()-1);
        }

        return null;
    }

    public String toString() {
        StringBuffer sb = new StringBuffer();

        Iterator iterator = arrayList.iterator();

        while(iterator.hasNext()) {

            String str = iterator.next().toString();
            sb.append(str + '\n');
        }

        return sb.toString();
    }
}
